#include "GameObjects.h"
#include "GameCore\tinyxml2\tinyxml2.h"
#include "EnemyShip.h"
#include "GameCore\Screen.h"
std::vector<ShipInitialValue> GameObjectFactory::shipInitialValueList;
void GameObject::Init()
{

}
void GameObject::Update(float timeDelta)
{
	if(destroyed && (!useAnim || animInst->IsStopped()))
	{
		removed = true;
	}
	position = position + move*timeDelta;
	if(useAnim)
	{
		animInst->Update(timeDelta);
	}
	if(position.y > Screen::GetInstance()->GetHeight() / 1.9f)
	{
		removed = true;
	}
}
void GameObject::Render()
{
	if(useAnim)
	{
		sprite->DrawAnim(animInst, position.x, position.y);
	}
	else
	{
		sprite->DrawFrame(frame, position.x, position.y);
	}
}
void GameObjectFactory::LoadShipInitialValue(const char* data)
{
	int len = strlen(data);
	tinyxml2::XMLDocument doc;
	doc.Parse( data );
	if(doc.ErrorID() != 0)
	{
		return;
	}
	tinyxml2::XMLElement* root = doc.FirstChildElement("root");
	tinyxml2::XMLElement* ship = root->FirstChildElement("ship");
	while(ship)
	{
		const char* shipName = ship->Attribute("name");
		const char* shipSprite = ship->Attribute("sprite");
		int hp = atoi(ship->Attribute("hp"));
		int damage = atoi(ship->Attribute("damage"));
		ShipInitialValue goiv(shipName,shipSprite,hp,damage);
		shipInitialValueList.push_back(goiv);
		ship = ship->NextSiblingElement("ship");
	}
	if(shipInitialValueList.size() > 1) //sort
	{
		for(int i = 0;i<shipInitialValueList.size()-1;i++)
		{
			std::vector<ShipInitialValue>::iterator it1 = shipInitialValueList.begin();
			std::vector<ShipInitialValue>::iterator it2 = it1 + 1;
			for(int j = 0;j<shipInitialValueList.size()-1-i;j++)
			{
				if(strcmp((*it1).name, (*it2).name) > 0)
				{
					ShipInitialValue temp = (*it1);
					(*it1) = (*it2);
					(*it2) = temp;
				}
				it1 = it2;
				it2++;
			}
		}
	}
	for(std::vector<ShipInitialValue>::iterator it = shipInitialValueList.begin(); it < shipInitialValueList.end();it++)
	{
		if(strcmp((*it).name, "EnemyRock") == 0)
		{
			(*it).createFunction = &(EnemyRock::CreateObject);
		}
	}
}
GameObject* GameObjectFactory::CreateShipFromName(const char* name)
{
	int left = 0;
	int right = shipInitialValueList.size() - 1;
	int mid = (left + right)/2;
	do
	{
		int cmp = strcmp(name, shipInitialValueList[mid].name);
		if(cmp < 0)
		{
			right = mid - 1;
			mid = (left + right)/2;
		}
		else if(cmp > 0)
		{
			left = mid + 1;
			mid = (left + right)/2;
		}
		else
		{
			Ship* ship = (Ship*) shipInitialValueList[mid].createFunction();
			//ship->sprite = shipInitialValueList[mid].sprite;
			return ship;
		}
	}
	while (left <= right);
	return NULL;
}
/*
void GameObjectFactory::UpdateShipInitialValueResource()
{
	for(std::vector<ShipInitialValue>::iterator it = shipInitialValueList.begin(); it < shipInitialValueList.end();it++)
	{
		if((*it).active)
		{
			(*it).sprite = SpriteManager::GetInstance()->GetSpriteByFileName((*it).spriteName);
		}
	}
}
*/
void GameObjectFactory::ActiveShipInitialValue(const char* name, bool active)
{
	int left = 0;
	int right = shipInitialValueList.size() - 1;
	int mid = (left + right)/2;
	do
	{
		int cmp = strcmp(name, shipInitialValueList[mid].name);
		if(cmp < 0)
		{
			right = mid - 1;
			mid = (left + right)/2;
		}
		else if(cmp > 0)
		{
			left = mid + 1;
			mid = (left + right)/2;
		}
		else
		{
			shipInitialValueList[mid].active = active;
			return;
		}
	}
	while (left <= right);
}
void GameObjectFactory::ActiveAllShipInitialValue(bool active)
{
	for(std::vector<ShipInitialValue>::iterator it = shipInitialValueList.begin(); it < shipInitialValueList.end();it++)
	{
		(*it).active = active;
	}
}
ShipInitialValue* GameObjectFactory::GetShipInitialValueFromName(const char* name)
{
	int left = 0;
	int right = shipInitialValueList.size() - 1;
	int mid = (left + right)/2;
	do
	{
		int cmp = strcmp(name, shipInitialValueList[mid].name);
		if(cmp < 0)
		{
			right = mid - 1;
			mid = (left + right)/2;
		}
		else if(cmp > 0)
		{
			left = mid + 1;
			mid = (left + right)/2;
		}
		else
		{
			return &(shipInitialValueList[mid]);
		}
	}
	while (left <= right);
	return NULL;
}